#include "AxisSettings.h"
#include <QMessageBox>
#include "../../common/utility.h"
#include "../MainWindow/MainWindow.h"

namespace LightChart
{
    AxisSettings::AxisSettings(QWidget* parent, Axis* axis) : QDialog(parent), axis(axis)
    {
	setupUi(this);
	connect(okButton, SIGNAL(clicked()), this, SLOT(onOkClicked()));
	connect(cancelButton, SIGNAL(clicked()), this, SLOT(onCancelClicked()));
	lineEditLabel->setText(axis->get_label().c_str());
	lineEditMin->setText(QString::number(axis->get_min()));
	lineEditMax->setText(QString::number(axis->get_max()));
	lineEditTics->setText(QString::number(axis->get_tics()));
    }
    void AxisSettings::onCancelClicked()
    {
	close();
    }
    void AxisSettings::onOkClicked()
    {
	axis->set_label(lineEditLabel->text().toStdString());
	try
	{
	    axis->set_max(QStringToDouble(lineEditMax->text()));
	    axis->set_min(QStringToDouble(lineEditMin->text()));
	    axis->set_tics(QStringToDouble(lineEditTics->text()));
	    close();
	}
	catch(const std::runtime_error& e)
	{
	    e.what();
	    QMessageBox::critical(this,"Input data corrupted.","Please insert valid number.");
	}
	static_cast<MainWindow*>(this->parentWidget())->RefreshAll();
    }
}
